"use strict"; // 开启严格模式

/* 
const isDrive = true;
let driveAge = 30;

if (isDrive) driveAge = 40; // 严格模式会扫描这里，即使进入不到这里, 反之如果不开启严格模式，并不会报错
if (driveAge) console.log("yes i can drive car:D");*/

/*
function logger() {
  console.log("My name is Joans");
}
logger();

// calling / running / invoking func Func declaration
function fruitProcessor(apples, oranges) {
  return `Juice with ${apples} apples and ${oranges} organes. `;
}

const applesJuice = fruitProcessor(5, 0);
console.log("声明式", applesJuice);

const appleOrangeJuice = fruitProcessor(2, 4);
console.log("声明式", appleOrangeJuice);

// Func expression
const calcAge2 = function (birthYear) {
  return 2037 - birthYear;
};
const age2 = calcAge2(1991);
console.log("表达式:", age2);

// Arrow Func
const calcAge3 = (birthYear) => 2037 - birthYear;
console.log(calcAge3(1991));

const yearsUnitilRetirement = (birthYear, firstName) => {
  const age = 2037 - birthYear;
  const retirement = 65 - age;
  return `${firstName} retires in ${retirement} years`;
};
console.log(1991, "Jonas");

// 高阶函数
const arrowFunc = (num1) => {
  return (num) => {
    if (num % 2 === 0) return num + num1;
  };
};

console.log(arrowFunc(3)(2));*/

/*
function fruitProcessor(apples, oranges, fruit) {
  const applePieces = fruit(apples);
  const orangePieces = fruit(oranges);
  return `Juice with ${applePieces} piece of apple and ${orangePieces} pieces of orange`;
}

console.log(fruitProcessor(2, 3, (fruit) => fruit * 4));

const calcAge = function (birthYear) {
  return 2037 - birthYear;
};

const yearsUnitilRetirement = function (birthYear, firstName) {
  const age = calcAge(birthYear);
  const retirement = 65 - age;
  if (retirement > 0) {
    console.log(`${firstName} retires in ${retirement} years`);
    return retirement;
  } else {
    console.log(`${firstName} has already retired 🥳`);
    return -1;
  }
};

console.log(yearsUnitilRetirement(1991, "Jonas"));
console.log(yearsUnitilRetirement(1950, "Jason"));*/

/*
const calcAverage = (a, b, c) => (a + b + c) / 2;
// test one
let scoreDolphins = calcAverage(44, 23, 71);
let scoreKoalas = calcAverage(65, 54, 49);

const checkWinner = function (avgDolphins, avgKoalas) {
  if (avgDolphins >= 2 * avgKoalas)
    console.log(`dolphins win 🥇 (${avgDolphins} vs. ${avgKoalas})`);
  else if (avgKoalas >= 2 * avgDolphins)
    console.log(`Koalas win🥇 (${avgKoalas} vs. ${avgDolphins})`);
  else console.log("No team wins...");
};

checkWinner(scoreDolphins, scoreKoalas);

checkWinner(576, 111);

// test two
scoreDolphins = calcAverage(85, 54, 41);
scoreKoalas = calcAverage(23, 34, 27);
console.log(scoreDolphins, scoreKoalas);
checkWinner(scoreDolphins, scoreKoalas);*/

/*
const friends = ["Micheal", "Jordan", "Peter", "Steven"];
console.log(friends);

const friendsMany = new Array("jason", "jonas");
// Add el 返回新的数组
friendsMany.push("Micheal");
friendsMany.unshift("Jay");
console.log(friendsMany);

//Remove el 返回删除的那个元素
const popEl = friendsMany.pop(); // Last
const shift = friendsMany.shift(); // Shift
console.log("popEl", popEl);
console.log("shiftEl", shift);

// Array.indexOf 找到了就返回对应元素的下标，没有找到就返回 -1
console.log(
  "查找元素,找到返回该元素下标, 反之返回-1",
  friendsMany.indexOf("jason")
);

// Es6 Array.includes() 是否包含？该元素，返回true or false
console.log(
  "是否包含该元素, 包含返回true, 反之返回false",
  friendsMany.includes("jonas")
);

const calcTip = function (bill) {
  return bill >= 50 && bill <= 300 ? bill * 0.15 : bill * 0.2;
};

const bills = [125, 555, 44];
const tips = [calcTip(bills[0]), calcTip(bills[1]), calcTip(bills[2])];
const totals = [bills[0] + tips[0], bills[1] + tips[1], bills[2] + tips[2]];

console.log(bills, tips, totals);*/

/*
const jason = {
  firstName: "Jason",
  lastName: "Schmedtmann",
  age: 32,
  job: "teacher",
  friends: ["Micheal", "Peter", "Steven"],
};
// console.log(jason);
console.log(jason.age);
// Obj的第二种访问，当你需要动态访问属性名的时候就可以使用中括号的形式
const nameKey = "Name";
console.log(jason["first" + nameKey]);
console.log(jason["last" + nameKey]);

// const interestedIn = prompt(
//   "What do you want to know about Jonas? Choose between firstName, lastName, age, job and friends"
// );

// if (jason[interestedIn]) console.log(jason[interestedIn]);
// else
//   console.log(
//     "Wrong request! Choose between firstName, lastName, age, job and friends"
//   );

console.log(
  `${jason.firstName} has ${jason.friends.length} and his best friend is called ${jason.friends[0]}`
);*/

/*
const jason = {
  firstName: "Jason",
  lastName: "Schmedtmann",
  birthYear: 1991,
  job: "teacher",
  friends: ["Micheal", "Peter", "Steven"],
  hasDriversLicense: false,
  calcAge() {
    this.age = 2037 - this.birthYear;
    return this.age;
  },
  getJasonInfo() {
    return `${this.firstName} is a ${this.calcAge()}-year old ${
      this.job
    }, and he has ${this.hasDriversLicense ? "a" : "no"} driver's license`;
  },
};

console.log(jason.getJasonInfo());*/

/*
const Mark = {
  fullName: "Mark Miller",
  mass: 78,
  height: 1.69,
  calcBMI() {
    return (this.bmi = this.mass / this.height ** 2);
  },
};

const john = {
  fullName: "John Smith",
  mass: 92,
  height: 1.95,
  calcBMI() {
    return (this.bmi = this.mass / this.height ** 2);
  },
};

Mark.calcBMI();
john.calcBMI();
console.log(parseInt(Mark.bmi), parseInt(john.bmi));

if (Mark.bmi > john.bmi)
  console.log(
    `${Mark.fullName}'s BMI (${Mark.bmi}) is higher than ${john.fullName}'s BMI ${john.bmi}`
  );
else if (john.bmi > Mark.bmi)
  console.log(
    `${john.fullName}'s BMI (${john.bmi}) is higher than ${Mark.fullName}'s BMI ${Mark.bmi}`
  );*/

/*
const jason = [
  "Jonas",
  "schemedtman",
  2037 - 1991,
  "teacher",
  true,
  ["Micheal", "Jorand", "steven"],
];

const types = [];
for (let index = 0; index < jason.length; index++) {
  types.push(typeof jason[index]);
}
console.log(types);

const years = [1991, 2007, 1969, 2020];
const ages = [];
for (let i = 0; i < years.length; i++) {
  ages.push(2037 - years[i]);
}
console.log(ages);

// continue and break
for (let i = jason.length - 1; i >= 0; i--) {
  console.log(i, jason[i]);
}

let randNumber = Math.trunc(Math.random() * 6);
while (1) {
  console.log(`You rolled a ${randNumber}`);
  randNumber = Math.trunc(Math.random() * 6);
  if (randNumber === 1) {
    console.log("Loop is about to end...");
    break;
  }
}
*/

const bills = [22, 295, 176, 550, 37, 105, 10, 1100, 86, 52];
const tips = [];
const totals = [];

const calctip = function (bill) {
  return bill >= 50 && bill <= 300 ? bill * 0.15 : bill * 0.2;
};

for (let index = 0; index < bills.length; index++) {
  tips.push(calctip(bills[index]));
  totals.push(tips[index] + bills[index]);
}
console.log(bills, tips, totals);

const calcAverage = function (arr) {
  let sum = 0;
  for (let i = 0; i < arr.length; i++) {
    sum += arr[i];
  }
  return sum / arr.length;
};

console.log("平均", calcAverage([2, 3, 7]));
console.log("平均小费", calcAverage(tips));
